<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="apply_status" label="申请状态">
        <uni-easyinput placeholder="申请状态,0:未审核,1:审核通过,2:审核不通过" type="number" v-model="formData.apply_status"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="id_front" label="身份证正面照片">
        <uni-data-checkbox :multiple="true" v-model="formData.id_front"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="id_back" label="身份证反面照片">
        <uni-data-checkbox :multiple="true" v-model="formData.id_back"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="real_name" label="真实姓名">
        <uni-easyinput placeholder="真实姓名" v-model="formData.real_name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="id_number" label="身份证号码">
        <uni-easyinput placeholder="身份证号码" v-model="formData.id_number"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="contact_phone" label="联系电话">
        <uni-easyinput placeholder="联系电话" v-model="formData.contact_phone"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="contact_address" label="联系地址">
        <uni-easyinput placeholder="联系地址" v-model="formData.contact_address"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="reason" label="申请理由">
        <uni-easyinput placeholder="申请理由" v-model="formData.reason"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="audit_time" label="审核时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.audit_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="创建时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="">
        <uni-easyinput placeholder="申请人id，关联用户表" v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/qmy-certification-application.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'qmy-certification-application';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "apply_status": 0,
        "id_front": [],
        "id_back": [],
        "real_name": "",
        "id_number": "",
        "contact_phone": "",
        "contact_address": "",
        "reason": "",
        "audit_time": null,
        "create_time": null,
        "user_id": ""
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("apply_status,id_front,id_back,real_name,id_number,contact_phone,contact_address,reason,audit_time,create_time,user_id").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
